using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;

using wnet.admin.Models;

using System;
using wnet.admin.wentcommon;
using Models;
using System.Web.UI.WebControls.WebParts;


namespace wnet.admin.Controllers.ywBusiness
{
    /// <summary>
    /// 专家系统文档+pdf
    /// </summary>
    [TokenAuth]
    [Route("business/ywexpertnew/{action}")]
    public class ywexpertnewController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public ywexpertnewController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }


        /// <summary>
        /// 专家系统-列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost]
        public object list(yw_expertQueryDto parm)
        {

            var predicate = Expressionable.Create<yw_expertnew>(); 
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.name), it => it.name.Contains(parm.name));
            var query = _sqlSugarClient.Queryable<yw_expertnew>()
                .Where(predicate.ToExpression())
                .Select((u) => new yw_expertnew
                {
                    id = u.id.SelectAll(),

                });

            var datalist = query.ToPage(parm);

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 专家系统-详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Getyw_expert(int id)
        {
            var response = _sqlSugarClient.Queryable<yw_expertnew>()
                .Where(q => q.id == id).First();

            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 专家系统-添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Addyw_expert([FromBody] yw_expertnew parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Insertable<yw_expertnew>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 专家系统-编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Updateyw_expert([FromBody] yw_expertnew parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Updateable<yw_expertnew>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 专家系统-删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Deleteyw_expert(int id)
        {
            var response = _sqlSugarClient.Queryable<yw_expertnew>()
                .Where(q => q.id == id).First();

            _sqlSugarClient.Deleteable<yw_expertnew>(response).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }
    }
}